/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package battleship;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import processing.core.*;
import javax.swing.*;


/**
 *
 * @author Code and Pseudocode: Ammar Ahmed, Shaun Pickford
 */
public class Main extends PApplet{
    

    /* Global Variables */
    static GameBoard gameBoard = new GameBoard();
    static int opponentHits = 0;
    static int playerHits = 0;
    static char whosTurn; //should store only 'p' or 'o' for player or opponent
    static int shotAfterHitIndex2;
    static AI aiObject = new AI();
    static int firstHit = 0;
    static int playerShots = 0;
    static int opponentShots = 0;

    /* Timer objects */
    Timer timer = new Timer(0, null);
    long startTime;

    /* Cell Grids */
    Cell P1Grid[];
    Cell AIGrid[];
    Cell P1Shots[];
    Cell AIShots[];



    /* Main Method */
    public static void main(String[] args)
    {
        PApplet.main(new String[]{"battleship.Main"} );
    }

    public void setup()
    {

        /* Create opponent game board */
        gameBoard.createGameBoard();
        //gameBoard.showOpponentGameBoard();
        //gameBoard.showGameBoard();
        whosTurn = 'p';

        /* Start timer */
        timer.start();
        startTime = System.currentTimeMillis();

        /* Initialize Grids */
        size(1000,700); //w h
        background(255);
        P1Grid = new Cell[100];
        AIGrid = new Cell[100];
        P1Shots = new Cell[100];
        AIShots = new Cell[100];

        /* Draw Grids */
        //draw all the grids nicely
        for(int j=0; j < 10; j++)
        {
           for(int i=0; i < 10; i++)
          {
              P1Grid[i+ (j*10)] = new Cell(i*20 + 50, j*20 + 10, 20 , 20);
              if (gameBoard.gameBoard[i+ (j*10)] != 'N')
              {
                  P1Grid[i+ (j*10)].isEmpty = false;
                  P1Grid[i+ (j*10)].color(  gameBoard.gameBoard[i+ (j*10)] );
              }

              /*AIGrid[i+ (j*10)] = new Cell(i*20 + width/2 + 100, j*20 + 10, 20 , 20);
              if (gameBoard.opponentGameBoard [i+ (j*10)] != 'N')
              {
                  AIGrid[i+ (j*10)].isEmpty = false;
                  AIGrid[i+ (j*10)].color(gameBoard.opponentGameBoard [i+ (j*10)]);
              }*/

              //shot records
              P1Shots[i+ (j*10)] = new Cell(i*20 + 50, j*20 + height/2 + 100, 20 , 20);
              if (gameBoard.shotRecord [i+ (j*10)] == true)
              {
                  P1Shots[i+ (j*10)].isEmpty = false;
                  P1Shots[i+ (j*10)].color(gameBoard.gameBoard[i+ (j*10)]);
              }

              AIShots[i+ (j*10)] = new Cell(i*20 + width/2 + 250, j*20 + height/2 + 100, 20 , 20);
              if (gameBoard.opponentShotRecord [i+ (j*10)] == true)
              {
                  AIGrid[i+ (j*10)].isEmpty = false;
                  AIGrid[i+ (j*10)].color(gameBoard.gameBoard[i+ (j*10)]);
              }
          }
         }

    }//setup

    public void draw()
    {


            refresh();
            Turn turn = new Turn();
            int shot = turn.startTurn(whosTurn); //return the location of the shot.
            turn.takeShot(shot, whosTurn);

            if (whosTurn == 'o')
            {
                P1Shots[shot-1].tested();
                fill(P1Shots[shot-1].R, P1Shots[shot-1].G, P1Shots[shot-1].B);
                rect(P1Shots[shot-1].x, P1Shots[shot-1].y, P1Shots[shot-1].w, P1Shots[shot-1].h);
                if (gameBoard.opponentGameBoard[shot-1] != 'N')
                {
                    P1Shots[shot-1].hit();
                    fill(P1Shots[shot-1].R, P1Shots[shot-1].G, P1Shots[shot-1].B);
                    rect(P1Shots[shot-1].x, P1Shots[shot-1].y, P1Shots[shot-1].w, P1Shots[shot-1].h);
                }
            }

            if (whosTurn == 'p')
            {

                AIShots[shot-1].tested();
                fill(AIShots[shot-1].R, AIShots[shot-1].G, AIShots[shot-1].B);
                rect(AIShots[shot-1].x, AIShots[shot-1].y, AIShots[shot-1].w, AIShots[shot-1].h);
                if (gameBoard.gameBoard[shot-1] != 'N')
                {
                   AIShots[shot-1].hit();
                   fill(AIShots[shot-1].R, AIShots[shot-1].G, AIShots[shot-1].B);
                   rect(AIShots[shot-1].x, AIShots[shot-1].y, AIShots[shot-1].w, AIShots[shot-1].h);
                }
            }

            if(playerHits >= 17 || opponentHits >= 17)
            {
                endGame();
            }


    }//draw

    public void refresh()
    {
        for(int i=0; i < 100; i++)
        {
            fill(P1Grid[i].R, P1Grid[i].G, P1Grid[i].B);
            rect(P1Grid[i].x ,P1Grid[i].y, P1Grid[i].w ,P1Grid[i].h);

            /*fill(AIGrid[i].R, AIGrid[i].G, AIGrid[i].B);
            rect(AIGrid[i].x, AIGrid[i].y, AIGrid[i].w, AIGrid[i].h);*/

            fill(P1Shots[i].R, P1Shots[i].G, P1Shots[i].B);
            rect(P1Shots[i].x, P1Shots[i].y, P1Shots[i].w, P1Shots[i].h);

            fill(AIShots[i].R, AIShots[i].G, AIShots[i].B);
            rect(AIShots[i].x, AIShots[i].y, AIShots[i].w, AIShots[i].h);
        }
    }

        


        /* Game Loop */
    /*
        while(playerHits < 17 && opponentHits < 17)
        {
                
               Turn turn = new Turn();

               int shot = turn.startTurn(whosTurn);

               turn.takeShot(shot, whosTurn);

        
        }//while someone has not won the game */

        private void endGame()
        {
            /* Gather timer information */
            timer.stop();
            long endTime = System.currentTimeMillis();
            long gameTime = (endTime - startTime) / 1000;
            double gameTimeMins = Math.floor(gameTime / 60);
            double gameTimeSecs = gameTime % 60;

            /* Display if player wins */
            if(playerHits >= 17)
            {
                JOptionPane.showMessageDialog(null, "You Win!");


            }

            /* Display if opponent wins */
            else
            {
                JOptionPane.showMessageDialog(null, "You Lose! Looks like a computer is smarter than you!");
            }

            /* Show game stats */
            JOptionPane.showMessageDialog(null, "Game Stats: \n\nPlayer Shots: " + playerShots + " Player hits: " + playerHits + " \n\nOpponent Shots: " + opponentShots + " Opponent hits: " + opponentHits + "\n\nGame Time: " + gameTimeMins + " Minutes " + gameTimeSecs + " Seconds", "Game Over", 1);
            System.exit(0);
        }
}//class
